<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB;
use App\Answer;
use App\Question;
use App\User;
use App\Comments;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //这里以后换成session;
        $user_id = 6;
    	//便利出所有的回答
    $res = DB::table("answer")->get();
    //dd($res);
    //空数组用于存回答和赞的数量
    $arr = array();
    //空数组用于存问题id和回答id
    $arr1 = array();
    //一个回答对应多个赞,一对多的关系
    //$model = new App\Answer();
    //循环出回答和赞的关系,回答的id作为下标,赞的数量作为值,组合成新数组
    foreach($res as $k=>$v)
    {
        $val = Answer::find($k+1)->getPraise()->where("apraise_status",1)->get()->count("*");
        $arr[$v->id] = $val;
    }
    //dd($arr);
    //对问题的赞,根据赞的数量进行逆向排序(从小到大)
    asort($arr);
    //dd($arr);
    $res1 = DB::table("question")->get();
    foreach($arr as $k2=>$v2)
    {
        $qid = DB::table("answer")->where("id",$k2)->first()->question_id;

        //将每个问题回答赞最高的存入新数组(回答已经根据赞进行逆向排序,此时只要根据问题的id进行去重,留下每个问题赞最高的回答
        //新数组,下标是问题的id,值为回答的id
        if(array_key_exists($qid, $arr1))
        {
            unset($arr1[$qid]);
        }
        $arr1[$qid] = $k2;
    }
    //dd($arr1);
    //对新数组进行反转
    //natsort($arr1);
    $arr1 = array_reverse($arr1,true);
   

    //dd($arr1);
    //查询对应问题来自哪个话题,一个问题对应多个话题,一个话题对应多个问题,属于多对多
    foreach($arr1 as $k=>$v)
    {
        //获取话题集合
        $arr3 = array();
        $topics = Question::find($k)->getTopicByQuestion()->get();
        //获取话题名字
        foreach($topics as $k1=>$v1)
        {
            $arr3[] = $v1->topic_name;
        }
        //将每个问题对应的话题存入新数组,下标是问题的id
        $arr2[$k] = $arr3;
        //根据回答的id去找是谁回答的
        $whoid = DB::table("answer")->where("id","$v")->first()->user_id;
        /**
         * 查询该用户是否对该问题是否关注了
         */
        $favor[$v] = DB::table("questionfocus")->where("user_id",$user_id)->where("question_id",$k)->first();
        /**
         * 根据回答者id寻找这个回答者的相关信息
         */
        //统计回答者回答了多少问题
        $answernum[$v] = User::find($whoid)->getAnswer()->where("user_id",$whoid)->get()->count("*");
        //统计回答者有多少关注者
         $focusnum[$v] = User::find($whoid)->getUserfocus()->where("other_id",$whoid)->get()->count("*");
         //统计该回答被评论了多少次
         $commentnum[$v] = Answer::find($v)->getComments()->where("answer_id",$v)->get()->count("*");
         //查询该回答评论的内容
         $commentcontent[$v] = Answer::find($v)->getComments()->where("answer_id",$v)->get();
         //查询该评论被攒了多少次
         foreach($commentcontent[$v] as $k1=>$v1)
         {
            $cprise[$v][$k1] = Comments::find($v1->id)->getPraiseByComments()->where("replay_id",$v1->id)->where("cpraise_status",1)->get()->count("*");
            //查询该用户的点赞对评论的状态
            $cpraise_status[$v][$k1] = Comments::find($v1->id)->getPraiseByComments()->where("replay_id",$v1->id)->where("cpraise_status",1)->where("user_id",$user_id)->get()->count("*");
            //统计评论者有多少关注者
            $focus_num[$v][$k1] = User::find($v1->user_id)->getUserfocus()->where("other_id",$v1->user_id)->get()->count("*");

             //统计评论者回答了多少问题
            $answer_num[$v][$k1] = User::find($v1->user_id)->getAnswer()->where("user_id",$v1->user_id)->get()->count("*");
         }
        //根据用户的id去找用户信息,存入数组,下标是回答的id
        $whoname[$v] = DB::table("user")->where("id",$whoid)->first()->username;
        //查找回答者的id
        $who_id[$v] = DB::table("user")->where("id",$whoid)->first()->id;
        //dd($whoname[$v]);
        //个人头像
        $user_portrait[$v] = DB::table("detail")->where("user_id",$whoid)->first()->user_portrait;
        //个人职业
        $professional[$v] = DB::table("detail")->where("user_id",$whoid)->first()->professional;
        //个人简介
        $introduction[$v] = DB::table("detail")->where("user_id",$whoid)->first()->introduction;

    }
    //根据回答的id去找是谁回答的
    //在这里,arr1是问题id对应回答id,arr是回答id对应赞的数量,arr2是每个问题对应的话题,下标是问题id,值是话题名字,二维数组,whoname,下标是回答的id,值是回答者姓名
    return view("Home.index",["user_id"=>$user_id,"arr1"=>$arr1,"arr"=>$arr,"arr2"=>$arr2,"whoname"=>$whoname,"professional"=>$professional,"user_portrait"=>$user_portrait,"introduction"=>$introduction,"answernum"=>$answernum,"focusnum"=>$focusnum,"commentnum"=>$commentnum,"favor"=>$favor,"commentcontent"=>$commentcontent,"cprise"=>$cprise,"cpraise_status"=>$cpraise_status,"focus_num"=>$focus_num,"answer_num"=>$answer_num,"who_id"=>$who_id]);
    // $model = new App\Answer();
    // foreach($arr as $k=>$v)
    // {
    //  $val = $model::find($k+1)->getPraise()->where("apraise_status",1)->get()->count("*");
    //  $arr1[] = $val;
    // }

    }
    //控制首页点赞
    public function apraise(Request $request)
    {
        $user_id = $request->input("user_id");
        $answer_id = $request->input("answer_id");
        $check = DB::table("apraise")->where("user_id",$user_id)->where("answer_id",$answer_id)->first();
        $val = $request->all();
        if(empty($check)&&empty($request->input("flag")))
        {
                $val["apraise_status"] = 1;
                $res = DB::table("apraise")->insert($val);
        }
        else
        {
            
                if($check->apraise_status == 1)
                {
                    $res = DB::table("apraise")->where("user_id",$user_id)->where("answer_id",$answer_id)->update(["apraise_status"=>0]);
                }
                else
                {
                    $res = DB::table("apraise")->where("user_id",$user_id)->where("answer_id",$answer_id)->update(["apraise_status"=>1]);
                }
            
        }
        if(!empty($request->input("flag")))
        {
            if(empty($check))
            {
                return false;
            }
            else
            {
                $res = DB::table("apraise")->where("user_id",$user_id)->where("answer_id",$answer_id)->update(["apraise_status"=>0]);
            }
        }
        $check = DB::table("apraise")->where("user_id",$user_id)->where("answer_id",$answer_id)->first();
        //返回数据库中当前用户对该回答的点赞情况  返回1 表示点赞 返回0 表示取消赞
        echo $check->apraise_status;
    }
    //首页收藏控制
    public function favor(Request $request)
    {
        $user_id = $request->input("user_id");
        $question_id = $request->input("question_id");
        $res = $request->all();
        $favor = DB::table("questionfocus")->where("user_id",$user_id)->where("question_id",$question_id)->first();
        if(!empty($favor))
        {
           $val = DB::table("questionfocus")->where("user_id",$user_id)->where("question_id",$question_id)->delete();
             echo "+ 关注问题";
        }
        else
        {
            $val = DB::table("collection")->insert($res);
            //session()->push('favor', $answer_id);
            //session()->flush();
            echo "取消关注";
        }
    }

    //回答的评论
    public function comments(Request $request)
    {
        $user_id = $request->input("user_id");
        $answer_id = $request->input("answer_id");
        $comments_content = $request->input("comments_content");
        $username = DB::table("user")->where("id",$user_id)->first()->username;
        $user_portrait = DB::table("detail")->where("id",$user_id)->first()->user_portrait;
        $res = DB::table("comments")->insert($request->all());
        $arr = array(1,$username,$user_portrait);
        if($res)
        {
            $val = json_encode($arr);
            echo $val;
            //echo 1;
        }
        else
        {
            echo 0;
        }
    }
    //评论的点赞
    public function cpraise(Request $request)
    {
        $user_id = $request->input("user_id");
        $replay_id = $request->input("replay_id");
        $val = $request->all();
        $res = DB::table("cpraise")->where("user_id",$user_id)->where("replay_id",$replay_id)->first();
        if(empty($res))
        {
            $val["cpraise_status"] = 1;
            $jg = DB::table("cpraise")->insert($val);
            echo 1;
        }
        else
        {
            if($res->cpraise_status == 1)
            {
               $val["cpraise_status"] =  0;
               $jg = DB::table("cpraise")->where("user_id",$user_id)->where("replay_id",$replay_id)->update($val); 
               echo 0;
            }
            else
            {
                $val["cpraise_status"] =  1;
               $jg = DB::table("cpraise")->where("user_id",$user_id)->where("replay_id",$replay_id)->update($val); 
               echo 1;
            }
            
        }
    }

    //查看其它用户的回答信息
    public function peopleanswer($id)
    {
        //这里以后换成session;
        $user_id = 6;
        $id = $id;
        //查询用户的基本细信息
        $user = DB::table("user")->where("id",$id)->first();
        //查询用户的详细信息
        $detail = DB::table("detail")->where("user_id",$id)->first();
        //查询该用户提问了多少问题
        $question_num = count(DB::table("question")->where("user_id",$id)->get());
        //查询该用户有多少回答
        $answer_num = count(DB::table("answer")->where("user_id",$id)->get());

         //查询该用户关注了多少问题
        $favor_num = count(DB::table("questionfocus")->where("user_id",$id)->get());
        $answers = DB::table("answer")->where("user_id",$id)->get();
        if(!empty($answers))
        { 
            foreach($answers as $k=>$v)
            {
                $question[$k] = $v->question_id;
                //查找该回答有多少评论
                $commentnum[$v->id] = Answer::find($v->id)->getComments()->where("answer_id",$v->id)->get()->count("*");
                //查询该回答的评论
                $comments[$v->id] = Answer::find($v->id)->getComments()->where("answer_id",$v->id)->get();
                //查询评论者的一些信息
                foreach($comments[$v->id] as $k2=>$v2)
                {
                    //统计评论者回答了多少问题
                    $commenter_answer_num[$v->id][$v2->id] = User::find($v2->user_id)->getAnswer()->where("user_id",$v2->user_id)->get()->count("*");
                    //统计评论者有多少关注者
                    $focus_num[$v->id][$v2->id] = User::find($v2->user_id)->getUserfocus()->where("other_id",$v2->user_id)->get()->count("*");

                    //查询该用户的点赞对评论的状态
                    $cpraise_status[$v->id][$v2->id] = Comments::find($v2->id)->getPraiseByComments()->where("replay_id",$v2->id)->where("cpraise_status",1)->where("user_id",$user_id)->get()->count("*");
                    //查询评论点赞数
                    $cprise[$v->id][$v2->id] = Comments::find($v2->id)->getPraiseByComments()->where("replay_id",$v2->id)->where("cpraise_status",1)->get()->count("*");
                }
              

                //检测当前浏览用户是否已经收藏
                $favor[$v->id] = DB::table("questionfocus")->where("user_id",$user_id)->where("question_id",$v->question_id)->first();
            }

            //查找对应问题详情 $questions存的是对应对题的详情 在模板里answer.blade.php 取$questions[$k]->id即可取得问题id
            foreach($question as $k=>$v)
            {
               $questions[$k] = DB::table("question")->where("id",$v)->first(); 
            }

          
            //解析页面
            return view("Home.people.answer",["user"=>$user,"user_id"=>$user_id,"id"=>$id,"detail"=>$detail,"question_num"=>$question_num,"answer_num"=>$answer_num,"favor_num"=>$favor_num,"answers"=>$answers,"questions"=>$questions,"commentnum"=>$commentnum,"favor"=>$favor,"comments"=> $comments,"commenter_answer_num"=>$commenter_answer_num,"focus_num"=>$focus_num,"cpraise_status"=>$cpraise_status,"cprise"=>$cprise]);
        }
        else
        {
             return view("Home.people.answer",["user"=>$user,"user_id"=>$user_id,"id"=>$id,"detail"=>$detail,"question_num"=>$question_num,"favor_num"=>$favor_num,"answers"=>$answers]);
        }
    }


    //关注他
    public function follow_him(Request $request)
    {
        $user_id = $request->input("user_id");
        $him_id = $request->input("other_id");
        $follow_him = $request->all();
        $res = DB::table("peoplefocus")->where("user_id",$user_id)->where("other_id",$him_id)->first();
        if(empty($res))
        {
            $val = DB::table("peoplefocus")->insert($follow_him);
            echo 1;
        }
        else
        {
            $val = DB::table("peoplefocus")->where("user_id",$user_id)->where("other_id",$him_id)->delete();
            echo 0;
        }
    }
    //用户关注列表
    public function peoplefollows($id)
    {
        //这里以后换成session;
        $user_id = 6;
        $id = $id;
         //查询用户的基本细信息
        $user = DB::table("user")->where("id",$id)->first();
        //查询用户的详细信息
        $detail = DB::table("detail")->where("user_id",$id)->first();
        //查询该用户提问了多少问题
        $question_num = count(DB::table("question")->where("user_id",$id)->get());
        //查询该用户有多少回答
        $answer_num = count(DB::table("answer")->where("user_id",$id)->get());
        //查询该用户有多少关注者
        $follows = DB::table("peoplefocus")->where("other_id",$id)->get();
        //查询该用户有多少收藏
        $favor_num = count(DB::table("questionfocus")->where("user_id",$id)->get());
        $answers = DB::table("answer")->where("user_id",$id)->get();
        //查询关注者的一些信息
        if(!empty($follows))
        {
                foreach($follows as $k=>$v)
                {
                    //查询关注者的一些基本信息
                    $follows_info[$v->id] = DB::table("detail")->where("user_id",$v->user_id)->first();
                    //查询关注者被多少人关注
                    $follows_info_num[$v->id] = count(DB::table("peoplefocus")->where("other_id",$v->user_id)->get());
                    //查询关注者提问了多少
                    $follows_question_num[$v->id] = count(DB::table("question")->where("user_id",$v->user_id)->get());
                    //查询关注者回答了多少问题
                     $follows_answer_num[$v->id] = count(DB::table("answer")->where("user_id",$v->user_id)->get());
                }
                 
                return view("Home.people.follow",["user"=>$user,"user_id"=>$user_id,"id"=>$id,"detail"=>$detail,"question_num"=>$question_num,"favor_num"=>$favor_num,"answers"=>$answers,"answer_num"=>$answer_num,"follows"=>$follows,"follows_info"=>$follows_info,"follows_info_num"=>$follows_info_num,"follows_question_num"=>$follows_question_num,"follows_answer_num"=>$follows_answer_num]);
        }
        else
        {
             return view("Home.people.follow",["user"=>$user,"user_id"=>$user_id,"id"=>$id,"detail"=>$detail,"question_num"=>$question_num,"favor_num"=>$favor_num,"answers"=>$answers,"answer_num"=>$answer_num,"follows"=>$follows]);
        }
    }
    //提问的问题列表
    public function peoplequestions($id){

         //这里以后换成session;
        $user_id = 6;
        $id = $id;
         //查询用户的基本细信息
        $user = DB::table("user")->where("id",$id)->first();
        //查询用户的详细信息
        $detail = DB::table("detail")->where("user_id",$id)->first();
        //查询该用户提问了多少问题
        $question_num = count(DB::table("question")->where("user_id",$id)->get());
        //查询该用户有多少回答
        $answer_num = count(DB::table("answer")->where("user_id",$id)->get());
        //查询该用户有多少关注者
        $follows = DB::table("peoplefocus")->where("other_id",$id)->get();
        //查询该用户有多少关注问题
        $favor_num = count(DB::table("questionfocus")->where("user_id",$id)->get());
        $answers = DB::table("answer")->where("user_id",$id)->get();
        //查询该用户的提问

       
         return view("Home.people.question",["user"=>$user,"user_id"=>$user_id,"id"=>$id,"detail"=>$detail,"question_num"=>$question_num,"favor_num"=>$favor_num,"answers"=>$answers,"answer_num"=>$answer_num,"follows"=>$follows]);
    }
}
